<template>
	<xn-form-container :title="formData.id ? '编辑内容管理' : '增加内容管理'" :width="900" v-model:open="open"
		:destroy-on-close="true" @close="onClose">
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="标题：" name="title">
				<a-input v-model:value="formData.title" placeholder="请输入标题" allow-clear />
			</a-form-item>
			<a-form-item label="所属栏目：" name="columnId">
				<a-select v-model:value="formData.columnId" placeholder="请输入所属栏目" :options="columnOptions"
					:field-names="{ label: 'columnName', value: 'id' }" />
			</a-form-item>
			<a-row>
				<a-col :span="12">
					<a-form-item label="类型：" name="type">
						<a-radio-group v-model:value="formData.type" placeholder="请选择类型" :options="typeOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="是否评分：" name="isRate">
						<a-radio-group v-model:value="formData.isRate" placeholder="是否评分" :options="linkOptions" />
					</a-form-item>
				</a-col>

			</a-row>

			<a-form-item label="描述：" name="description">
				<a-textarea type="textarea" placeholder="请输入描述" allow-clear v-model:value="formData.description" />
			</a-form-item>
			<a-form-item label="图片：" name="description">
				<xn-upload-image v-model="formData.cover"></xn-upload-image>
			</a-form-item>

			<a-row :gutter="24">
				<a-col :span="12">
					<a-form-item label="内容：" name="content">
						<a-textarea v-if="formData.type == '1'" v-model:value="formData.content" placeholder="请输入内容"
							allow-clear />
						<xn-editor v-model="formData.content" placeholder="请输入正文" :height="708"
							v-else-if="formData.type == '2'" :file-upload-function="apiFunction.fileUploadApi" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="内容：" name="content">
						<div class="demo-model">
							<iframe :src="formData.content" frameborder="0" v-if="formData.type == '1'"></iframe>
							<div class="hm" v-html="formData.content" v-else-if="formData.type == '2'">

							</div>
						</div>
					</a-form-item>
				</a-col>
			</a-row>



		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizNewContentForm">
import tool from '@/utils/tool'
import { cloneDeep } from 'lodash-es'
import { required } from '@/utils/formRules'
import bizNewContentApi from '@/api/biz/bizNewContentApi'
import { toRaw } from 'vue'
import fileApi from '@/api/dev/fileApi'
import XnEditor from '@/components/Editor/index.vue'
import XnUploadImage from '@/components/XnUploadImage/index.vue'
// 抽屉状态
const open = ref(false)
const emit = defineEmits({ successful: null })
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const typeOptions = ref([])
const linkOptions = ref([])
const columnOptions = ref([])
// 打开抽屉
const onOpen = (record, columns) => {
	open.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	} else {
		formData.value = Object.assign({}, { type: '1' ,isRate:'1'})
	}
	columnOptions.value = toRaw(columns)

	typeOptions.value = tool.dictList('CONTENT_TYPE')
	linkOptions.value = tool.dictList('IS_RATE')
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	open.value = false
}
// 默认要校验的
const formRules = {
	title: [required('请输入标题')],
	columnId: [required('请输入所属栏目')],
	type: [required('请输入类型')],
	content: [required('请输入内容')],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			bizNewContentApi
				.bizNewContentSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
		.catch(() => { })
}

// 传递文件上传需要的API
const apiFunction = {
	fileUploadApi: (param) => {
		return fileApi.fileUploadDynamicReturnUrl(param).then((data) => {
			return Promise.resolve(data)
		})
	}
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
<style lang="less" scoped>
.demo-model {
	font-size: 16px;
	background-color: #fff;
	width: 330px;
	z-index: 10;
	margin: 0;
	top: calc(50vh - 280px + 1.8rem);
	box-sizing: border-box;
	overflow-y: auto;
	background-image: url(/img/iPhoneX_model.png);
	background-repeat: no-repeat;
	background-size: 100%;
	border-radius: 30px;
	padding: 48px 23px 38px 16px;

}

.demo-model {
	width: 350px;
	height: 708.88575px;
	top: calc(50vh - 354.44288px + 1.8rem);
}
</style>